package com.framework.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.framework.common.auto.ThreadLocalUtils;
import com.framework.common.dto.PageDTO;
import com.framework.core.entity.SysLog;
import com.framework.core.service.SysLogService;
import com.framework.mybatis.entity.BaseEntity;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author lihanbo
 * @date 2021/8/27 13:42
 */
@Service("coreSysLogService")
@Slf4j
public class SysLogServiceImpl extends BaseServiceImpl<SysLog> implements SysLogService {

    @Async
    @Override
    public void saveSysLog(SysLog webLog) {
        try {
            this.save(webLog);
//            Map<String, Object> stringObjectMap = BeanUtil.beanToMap(webLog);
//            stringObjectMap.put("createDate", DateUtil.date().toString());
//            MongodbUtils.save(stringObjectMap, "sysLog");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    @Override
    public QueryWrapper<SysLog> getWhere(PageDTO pageDto) {
        QueryWrapper<SysLog> where = new QueryWrapper<>();
        where.lambda().eq(BaseEntity::getCorpId, ThreadLocalUtils.getCorpId())
                .and(StrUtil.isNotEmpty(pageDto.getSearchText()), x -> x.like(SysLog::getUserid, pageDto.getSearchText())
                        .or().like(SysLog::getName, pageDto.getSearchText())
                        .or().like(SysLog::getOperation, pageDto.getSearchText())
                        .or().like(SysLog::getUrl, pageDto.getSearchText())
                        .or().like(SysLog::getParams, pageDto.getSearchText())
                        .or().like(SysLog::getOsName, pageDto.getSearchText()))
                .and(x -> x.between(BaseEntity::getCreateDate, pageDto.getStartTime() + " 00:00:00", pageDto.getEndTime() + " 23:59:59"))
                .orderByDesc(BaseEntity::getCreateDate);
        return where;
    }
}
